const CommonApi = require('../../../api/common.js');
const UserApi = require('../../../api/user.js');
const regExpUtil = require('../../utils/regExp.js');

Page({
  data: {
    tickTip: '获取验证码',
    dialogVisible: false,
    // formModel--start
    phoneNumber: null,
    code: null,
    captcha: null,
    captchaId: null,
    // formModel--end
    redirect_uri: '/pages/index/index',
    redirect_uri_type: 'tab',
    msgSend: false,
    isAgree: false,
    timers: 60,
    countDown: null,
  },

  onLoad(options) {
    if (options.redirect_uri) {
      this.setData({redirect_uri: options.redirect_uri})
    }
    if (options.redirect_uri_type) {
      this.setData({redirect_uri_type: options.redirect_uri_type})
    }
  },

  onReady() {

  },

  onShow() {
    wx.removeStorageSync('token')
    wx.removeStorageSync('userInfo')
    wx.removeStorageSync('userService')
  },

  onHide() {

  },
  handleInput(e){
    let target = e.currentTarget.dataset.item;
    let value = e.detail.value;
    if (target == 'phoneNumber') {
      this.setData({phoneNumber: value})
    } else if (target == 'code') {
      this.setData({code: value})
    }
  },
  checkboxChange(e){
    let value = e.detail.value;
    this.setData({isAgree: value.length > 0})
  },
  startInterval(){
    let that = this;
    let countDownNum = that.data.timers;

    that.data.countDown = setInterval(()=>{
      if (countDownNum < 1) {
        that.setData({tickTip: '获取验证码', timers: 60});
        clearInterval(that.data.countDown);
      } else {
        countDownNum--;
        that.setData({
          timers: countDownNum,
          tickTip: countDownNum+'s后重发'
        })
      }
    }, 1000)
  },
  getVerifyCode(){
    if (this.data.tickTip != '获取验证码') return;
    if (!this.data.phoneNumber) {
      wx.showToast({icon: 'none', title: '请输入手机号码'})
    } else if (this.data.phoneNumber && !regExpUtil.isPhoneNumber(this.data.phoneNumber)) {
      wx.showToast({icon: 'none', title: '请输入正确的手机号码'})
    } else {
      this.setData({dialogVisible: true});
    }
  },
  // 获取图形验证码弹窗传递来的数据
  getCaptchaData(e){
    console.log(e);
    this.setData({
      dialogVisible: false, 
      captcha: e.detail.captcha,
      captchaId: e.detail.captchaId
    })
    this.sendSms()
  },
  // 发送短信验证码
  sendSms(){
    CommonApi.sendSms({phoneNumber: this.data.phoneNumber, code: this.data.captcha, captchaId: this.data.captchaId}).then(res=>{
      if (res.retCode == "000000" && res.errCode == "000000") {
        this.setData({msgSend: true});
        wx.showToast({icon: 'success', title: '短信验证码发送成功'});
        this.startInterval();
      }
    })
  },
  // 校验短信验证码
  checkSms(){
    CommonApi.checkSms({phoneNumber: this.data.phoneNumber, code: this.data.code}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        console.log(res);
        if (!res.data) {
          wx.showToast({icon: 'error', title: '验证码错误'});
        } else {
          this.submitForm()
        }
      }
    })
  },
  submitForm(){
    if (!this.data.isAgree) {
      wx.showToast({icon: 'none', title: '请勾选阅读并接受用户协议'})
    } else {
      let formModel = {code: this.data.code, phoneNumber: this.data.phoneNumber, sourceSite: 'quotation', loginFrom: 'xcx'};
      UserApi.phoneRegister({...formModel}).then(res=>{
        if (res.retCode == "000000" && res.errCode == "000000") {
          wx.showToast({icon: 'success', title: res.errMsg});

          CommonApi.phoneLogin({...formModel}).then(loginRes=>{
            if (loginRes.retCode == "000000" && loginRes.errCode == "000000") {
              wx.setStorageSync('token', loginRes.data.accessToken);
              wx.setStorageSync('lastLoginInfo', {phoneNumber: this.data.phoneNumber});

              UserApi.getLoginUser().then(userRes=>{
                if (userRes.retCode == "000000" && userRes.errCode == "000000") {
                  wx.setStorageSync('userInfo', userRes.data)
                  if (this.data.redirect_uri_type == 'tab') {
                    wx.switchTab({url: this.data.redirect_uri})
                  } else if (this.data.redirect_uri_type == 'link') {
                    wx.redirectTo({url: this.data.redirect_uri})
                  }
                }
              })
            }
          })
        } else {
          wx.showToast({icon: 'error', title: res.errMsg});
        }
      })
    }
  },
  formSubmit(e){
    console.log(e.detail.value);
    if (!this.data.phoneNumber) {
      wx.showToast({icon: 'none', title: '请输入手机号码'})
    } else if (this.data.phoneNumber && !regExpUtil.isPhoneNumber(this.data.phoneNumber)) {
      wx.showToast({icon: 'none', title: '请输入正确的手机号码'})
    } else if (!this.data.code) {
      wx.showToast({icon: 'none', title: '请输入验证码'})
    } else {
      this.checkSms();
    }
  },
  checkProtocol(){
    wx.navigateTo({
      url: '/packageA/pages/protocol/index',
    })
  },
  toLogin(){
    wx.redirectTo({
      url: '/packageA/pages/login/index?redirect_uri='+this.data.redirect_uri+'&redirect_uri_type='+this.data.redirect_uri_type,
    })
  },

  onUnload() {

  },
})